﻿using System;
using System.Collections.Generic;
using System.Text;
using hedefgrup.reports.renault.database;
using System.Data;

namespace hedefgrup.reports.common
{
    public abstract class Criteria_Month : Criteria
    {
        public Criteria_Month()
        {
            //DataTable yearsAndMonthsData = db.query("SELECT AY, YIL FROM " + Context.TableName + " (NOLOCK) GROUP BY AY, YIL ORDER BY YIL DESC, AY DESC");
            //Values.Add(string.Format("{0:0000}{1:00}", yearsAndMonthsData.Rows[0]["YIL"], yearsAndMonthsData.Rows[0]["AY"]));
        }

        public override string Expression
        {
            get { return "YIL * 12 + AY"; }
        }

        public override Type ValueType
        {
            get { return typeof(string); }
        }

        public override string[] GetValueList()
        {
            List<string> yearsAndMonths = new List<string>();
            DataTable yearsAndMonthsData = db.query("SELECT AY, YIL FROM " + Context.TableName + " (NOLOCK) GROUP BY AY, YIL ORDER BY YIL DESC, AY DESC");
            foreach (DataRow yearAndMonthRow in yearsAndMonthsData.Rows)
            {
                string rowValue = string.Format("{0:0000}{1:00}", yearAndMonthRow["YIL"], yearAndMonthRow["AY"]);
                if (Values == null || Values.Count == 0)
                {
                    Values.Add(rowValue);
                }
                yearsAndMonths.Add(rowValue);
            }
            return yearsAndMonths.ToArray();
        }

        public override object ToActualValue()
        {
            string year = Values[0].Substring(0, 4);
            string month = Values[0].Substring(4, 2);
            return Convert.ToInt32(year) * 12 + Convert.ToInt32(month);
        }

        public override object[] ToActualValues()
        {
            return new object[] { ToActualValue() };
        }
    }
}
